<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

use app\common\model\ForumBbs;
use app\common\model\ForumBbsComment;

/**
 * 平台逻辑
 */
class Platform extends AdminBase
{
    
    /**
     * 平台分类编辑
     */
    public function platformCategoryEdit($data = [])
    {
        
        $validate_result = $this->validatePlatformCategory->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validatePlatformCategory->getError()];
        }
        
        $url = url('platformCategoryList');
        
        $result = $this->modelExchangePlatformCategory->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '平台分类' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelExchangePlatformCategory->getError()];
    }
    
    /**
     * 获取平台列表
     */
    public function getPlatformList($data = [], $field = 'a.*,m.nickname,c.name as category_name', $order = '')
    {


        $this->modelExchangePlatform->alias('a');
        if(!empty($data['tags'])){
            $where['a.tags']=['like','%,'.$data['tags'].',%'];
        }
       
        if(!empty($data['search_data'])){
            $where['a.name | a.content']=['like','%'.$data['search_data'].'%'];
        }
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        $count=$this->modelExchangePlatform->alias('a')->where($where)->count();
        $list=$this->modelExchangePlatform->alias('a')->field($field)->where($where)->paginate(50,false,['query'=>$data]);

        return ['list'=>$list,'count'=>$count];
    }

    


    /**
     * 获取平台列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.name|a.describe'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
    /**
     * 平台信息编辑
     */
    public function platformEdit($data = [])
    {

        $validate_result = $this->validatePlatform->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validatePlatform->getError()];
        }
        
        $url = url('platformList');
        
       // empty($data['id']) && $data['member_id'] = MEMBER_ID;
        
//      $data['content'] = htmlspecialchars_decode($data['content']);
        preg_match_all('/(?<=img.src=\/").*?(?=")/', $data['content'], $out, PREG_PATTERN_ORDER);      //正则匹配img标签的src属性，返回二维数组
        if (!empty($out)) {
            foreach ($out as $v) {
                foreach ($v as $j) {
                    $url1 = DMOAIN_URL.$j;
                    $data['content'] = str_replace($j, $url1, $data['content']);   //替换相对路径为绝对路径
                }
            }
        }
        $result = $this->modelExchangePlatform->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '平台' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '平台操作成功', $url] : [RESULT_ERROR, $this->modelExchangePlatform->getError()];
    }

    /**
     * 获取平台信息
     */
    public function getplatformInfo($where = [], $field = 'a.*,m.nickname,c.name as category_name')
    {
        
        $this->modelExchangePlatform->alias('a');
        

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        

        
        return $this->modelExchangePlatform->getInfo($where, $field);
    }
    
    /**
     * 获取分类信息
     */
    public function getplatformCategoryInfo($where = [], $field = true)
    {
        
        return $this->modelExchangePlatformCategory->getInfo($where, $field);
    }
    
    /**
     * 获取平台分类列表
     */
    public function getplatformCategoryList($where = [], $field = true, $order = '', $paginate = 0)
    {
        
        return $this->modelExchangePlatformCategory->getList($where, $field, $order, $paginate);
    }
    
    /**
     * 平台分类删除
     */
    public function platformCategoryDel($where = [])
    {
        
        $result = $this->modelExchangePlatformCategory->deleteInfo($where);
        
        $result && action_log('删除', '平台分类删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '平台分类删除成功'] : [RESULT_ERROR, $this->modelExchangePlatformCategory->getError()];
    }
    
    /**
     * 平台删除
     */
    public function platformDel($where = [])
    {
        
        $result = $this->modelExchangePlatform->deleteInfo($where);
        
        $result && action_log('删除', '平台删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '平台删除成功'] : [RESULT_ERROR, $this->modelExchangePlatform->getError()];
    }

    /**
     * 审核平台
     */
    public function update_info($where = [])
    {

        if(isset($where['ids']) && is_array($where['ids'])){
            foreach ($where['ids'] as $k =>$v){
                $result= ForumBbs::update([
                    'id'=>$v['value'],
                    'state'=>$where['status'],
                    'update_time'=>time()
                ]);
            }
        }else{
            $result = $this->modelExchangePlatform->setInfo($where);
        }




        return $result ? [RESULT_SUCCESS, '操作成功'] : [RESULT_ERROR, '操作失败'];
    }


    /**
     * 回帖隐藏显示
     */
    public function comment_update_info($where = [])
    {

        if(isset($where['ids']) && is_array($where['ids'])){
            foreach ($where['ids'] as $k =>$v){
                $result= ForumBbsComment::update([
                    'id'=>$v['value'],
                    'is_hide'=>$where['status'],
                    'update_time'=>time()
                ]);
            }
        }else{
            $result = $this->modelExchangePlatformComment->setInfo($where);
        }




        return $result ? [RESULT_SUCCESS, '操作成功'] : [RESULT_ERROR, '操作失败'];
    }


    /**
     * 平台标签编辑
     */
    public function platformTagsEdit($data = [])
    {

        $validate_result = $this->validateExchangeTags->scene('edit')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateExchangeTags->getError()];
        }

        $url = url('platformTagsList');;
        $result = $this->modelExchangeTags->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '平台标签' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelExchangeTags->getError()];
    }


    /**
     * 获取标签信息
     */
    public function getplatformTagsInfo($where = [], $field = true)
    {

        return $this->modelExchangeTags->getInfo($where, $field);
    }

    /**
     * 获取平台标签列表
     */
    public function getplatformTagsList($where = [], $field = true, $order = '', $paginate = 0)
    {

        return $this->modelExchangeTags->getList($where, $field, $order, $paginate);
    }

    /**
     * 平台标签删除
     */
    public function platformTagsDel($where = [])
    {

        $result = $this->modelExchangeTags->deleteInfo($where);

        $result && action_log('删除', '平台标签删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '平台标签删除成功'] : [RESULT_ERROR, $this->modelExchangeTags->getError()];
    }
}
